<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--课程结构数据-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input
            v-model="conName"
            placeholder="请输入课程结构名称"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container">
          <el-tree
            :data="stemCourseConOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            node-key="id"
            default-expand-all
            highlight-current
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>
      <!--课程数据-->
      <el-col :span="20" :xs="24">
        <el-form
          :model="queryParams"
          ref="queryForm"
          size="small"
          :inline="true"
          v-show="showSearch"
          label-width="120px"
        >
          <el-form-item label="课程名称" prop="courseName">
            <el-input
              v-model="queryParams.courseName"
              placeholder="请输入课程名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <!--        <el-form-item label="属于STEM中哪个学科" prop="subjectCategory">-->
          <!--          <el-select v-model="queryParams.subjectCategory" placeholder="请选择课程属于STEM中的哪个学科" clearable>-->
          <!--            <el-option-->
          <!--              v-for="dict in dict.type.subject_category"-->
          <!--              :key="dict.value"-->
          <!--              :label="dict.label"-->
          <!--              :value="dict.value"-->
          <!--            />-->
          <!--          </el-select>-->
          <!--        </el-form-item>-->
          <el-form-item label="课程难度等级" prop="difficultyLevel">
            <el-select
              v-model="queryParams.difficultyLevel"
              placeholder="请选择课程的难度等级"
              clearable
            >
              <el-option
                v-for="dict in dict.type.difficulty_level"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
            >搜索
            </el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置
            </el-button
            >
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['stemCourse:stemCourse:add']"
            >新增
            </el-button>
          </el-col>
          <!--        <el-col :span="1.5">-->
          <!--          <el-button-->
          <!--            type="success"-->
          <!--            plain-->
          <!--            icon="el-icon-edit"-->
          <!--            size="mini"-->
          <!--            :disabled="single"-->
          <!--            @click="handleUpdate"-->
          <!--            v-hasPermi="['stemCourse:stemCourse:edit']"-->
          <!--          >修改-->
          <!--          </el-button>-->
          <!--        </el-col>-->
          <!--        <el-col :span="1.5">-->
          <!--          <el-button-->
          <!--            type="danger"-->
          <!--            plain-->
          <!--            icon="el-icon-delete"-->
          <!--            size="mini"-->
          <!--            :disabled="multiple"-->
          <!--            @click="handleDelete"-->
          <!--            v-hasPermi="['stemCourse:stemCourse:remove']"-->
          <!--          >删除-->
          <!--          </el-button>-->
          <!--        </el-col>-->
          <!--        <el-col :span="1.5">-->
          <!--          <el-button-->
          <!--            type="primary"-->
          <!--            plain-->
          <!--            icon="el-icon-plus"-->
          <!--            size="mini"-->
          <!--            @click="handleAdd"-->
          <!--            v-hasPermi="['stemCourse:stemCourse:teachingPlan']"-->
          <!--          >组成教案-->
          <!--          </el-button>-->
          <!--        </el-col>-->
          <!--        <el-col :span="1.5">-->
          <!--          <el-button-->
          <!--            type="warning"-->
          <!--            plain-->
          <!--            icon="el-icon-download"-->
          <!--            size="mini"-->
          <!--            @click="handleExport"-->
          <!--            v-hasPermi="['stemCourse:stemCourse:export']"-->
          <!--          >导出-->
          <!--          </el-button>-->
          <!--        </el-col>-->
          <right-toolbar
            :showSearch.sync="showSearch"
            @queryTable="getList"
          ></right-toolbar>
        </el-row>

        <!-- <el-table
          v-loading="loading"
          :data="stemCourseList"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="课程结构" align="center" prop="conName" />
          <el-table-column label="课程名称" align="center" prop="courseName" />
          <el-table-column
            label="STEM学科"
            align="center"
            prop="subjectCategory"
          >
            <template slot-scope="scope">
              <dict-tag
                :options="dict.type.subject_category"
                :value="scope.row.subjectCategory"
              />
            </template>
          </el-table-column>
          <el-table-column
            label="课程难度等级"
            align="center"
            prop="difficultyLevel"
          >
            <template slot-scope="scope">
              <dict-tag
                :options="dict.type.difficulty_level"
                :value="scope.row.difficultyLevel"
              />
            </template>
          </el-table-column>
          <el-table-column
            label="适合学生年级"
            align="center"
            prop="recommendedGrade"
          />
          <el-table-column label="学分" align="center" prop="credits" />
          <el-table-column
            label="大致完成时间"
            align="center"
            prop="duration"
          />
          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="skipQuestion(scope.row)"
                v-hasPermi="['stemCourse:stemCourse:question']"
                >题库
              </el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['stemCourse:stemCourse:edit']"
                >修改
              </el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['stemCourse:stemCourse:remove']"
                >删除
              </el-button>
            </template>
          </el-table-column>
        </el-table> -->
        <div style="display: flex; flex-direction: row; flex-wrap: wrap">
          <div
            v-for="(item, index) in stemCourseList"
            :key="index"
            @click="onclick(item)"
          >
            <el-card class="box-card" shadow="always">
              <div slot="header" class="clearfix">
                <span>{{ item.courseName }}</span>
                <el-button
                  style="float: right; margin: 0 3px"
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click.stop="handleDelete(item)"
                  v-hasPermi="['stemCourse:stemCourse:remove']"
                >删除
                </el-button>
                <el-button
                  style="float: right; margin: 0 3px"
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click.stop="handleUpdate(item)"
                  v-hasPermi="['stemCourse:stemCourse:edit']"
                >修改
                </el-button>
                <el-button
                  style="float: right; margin: 0 3px"
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click.stop="skipQuestion(item)"
                  v-hasPermi="['stemCourse:stemCourse:question']"
                >题库
                </el-button>
              </div>
              <div class="text item">课程结构：{{ item.conName }}</div>
              <div class="text item">课程难度等级：{{ item.difficultyLevel }}</div>
              <div class="text item">适合学生年级：{{ item.recommendedGrade }}</div>
              <div class="text item">学分：{{ item.credits }}</div>
              <div class="text item">大致完成时间：{{ item.duration }}</div>
              <div class="text item">STEM学科：
                <dict-tag style="display: inline-block;"
                  :options="dict.type.subject_category"
                  :value="item.subjectCategory"
                />
              </div>
            </el-card>
          </div>
        </div> 
        <el-empty
          v-if="stemCourseList.length == 0 || stemCourseList == null"
          description="暂无数据"
        ></el-empty>

        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <!-- 添加或修改STEM课程对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="180px">
        <el-form-item label="课程结构" prop="conId">
          <treeselect
            v-model="form.conId"
            :options="stemCourseConOptions"
            :show-count="true"
            placeholder="请选择归属课程结构"
          />
        </el-form-item>
        <!--        <el-form-item label="课程结构" prop="conId">-->
        <!--          <el-input v-model="form.conId" placeholder="请输入课程结构"/>-->
        <!--        </el-form-item>-->
        <el-form-item label="课程名称" prop="courseName">
          <el-input v-model="form.courseName" placeholder="请输入课程名称"/>
        </el-form-item>
        <el-form-item label="STEM中的哪个学科" prop="subjectCategory">
          <el-select
            v-model="form.subjectCategory"
            placeholder="请选择课程属于STEM中的哪个学科"
          >
            <el-option
              v-for="dict in dict.type.subject_category"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="课程难度等级" prop="difficultyLevel">
          <el-select
            v-model="form.difficultyLevel"
            placeholder="请选择课程的难度等级"
          >
            <el-option
              v-for="dict in dict.type.difficulty_level"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="适合的学生年级" prop="recommendedGrade">
          <el-input
            v-model="form.recommendedGrade"
            placeholder="请输入课程适合的学生年级"
          />
        </el-form-item>
        <el-form-item label="学分" prop="credits">
          <el-input-number v-model="form.credits" placeholder="请输入学分"/>
        </el-form-item>
        <el-form-item label="课程大致完成时间" prop="duration">
          <el-input
            v-model="form.duration"
            placeholder="请输入课程的大致完成时间"
          />
        </el-form-item>
        <el-form-item label="课程内容" prop="courseDescription">
          <editor v-model="form.courseDescription" :min-height="192"/>
          <!--          <el-input-->
          <!--            v-model="form.courseDescription"-->
          <!--            type="textarea"-->
          <!--            placeholder="请输入内容"-->
          <!--          />-->
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listStemCourse,
  getStemCourse,
  delStemCourse,
  addStemCourse,
  updateStemCourse,
  stemCourseConTreeSelect,
} from "@/api/stemCourse/stemCourse";
import {delStemCourseCon} from "@/api/stemCourseCon/stemCourseCon";
import Treeselect from "@riophae/vue-treeselect";
import {deptTreeSelect} from "@/api/system/user";

export default {
  name: "StemCourse",
  components: {Treeselect},
  dicts: ["difficulty_level", "subject_category"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // STEM课程表格数据
      stemCourseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 9,
        courseName: null,
        subjectCategory: null,
        difficultyLevel: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      conName: undefined,
      stemCourseConOptions: undefined,
      defaultProps: {
        children: "children",
        label: "label",
      },
    };
  },
  created() {
    this.getList();
    this.getStemCourseConTree();
  },
  methods: {
    // 查看课程详情
    onclick(item) {
      console.log(item, "shuju");
      this.$router.push({
        path: "/stem/courseDetail",
        query: {courseId: item.courseId},
      });
    },
    skipQuestion(row) {
      this.$router.push({
        path: "/stem/question",
        query: {courseId: row.courseId},
      });
    },
    /** 查询部门下拉树结构 */
    getStemCourseConTree() {
      stemCourseConTreeSelect().then((response) => {
        this.stemCourseConOptions = response.data;
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.conId = data.id;
      this.handleQuery();
    },
    delStemCourseCon,
    /** 查询STEM课程列表 */
    getList() {
      this.loading = true;
      listStemCourse(this.queryParams).then((response) => {
        this.stemCourseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        courseId: null,
        conId: null,
        courseName: null,
        courseDescription: null,
        subjectCategory: null,
        difficultyLevel: null,
        recommendedGrade: null,
        credits: null,
        duration: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.courseId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加STEM课程";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const courseId = row.courseId || this.ids;
      getStemCourse(courseId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改STEM课程";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.courseId != null) {
            updateStemCourse(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStemCourse(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const courseIds = row.courseId || this.ids;
      this.$modal
        .confirm('是否确认删除STEM课程编号为"' + courseIds + '"的数据项？')
        .then(function () {
          return delStemCourse(courseIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "stemCourse/stemCourse/export",
        {
          ...this.queryParams,
        },
        `stemCourse_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>

<style scoped>
.box-card {
  width: 413px;
  margin: 16px 20px 0 0;
  cursor: pointer;
  border-radius: 15px;
}
</style>
